package com.jyx.manage_course.controller;

import com.jyx.api.course.CourseControllerApi;
import com.jyx.framework.domain.course.*;
import com.jyx.framework.domain.course.ext.CourseView;
import com.jyx.framework.domain.course.ext.TeachplanNode;
import com.jyx.framework.domain.course.request.CourseListRequest;
import com.jyx.framework.domain.course.response.CoursePublishResult;
import com.jyx.framework.exception.ExceptionCast;
import com.jyx.framework.model.response.CommonCode;
import com.jyx.framework.model.response.QueryResponseResult;
import com.jyx.framework.model.response.ResponseResult;
import com.jyx.framework.utils.JyxOauth2Util;
import com.jyx.manage_course.service.CourseMarketService;
import com.jyx.manage_course.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/course")
public class CourseController implements CourseControllerApi {

    @Autowired
    CourseService courseService;
    @Autowired
    CourseMarketService courseMarketService;

    @Override
    @GetMapping("/teachplan/list/{courseId}")
    public TeachplanNode findTeachplanList(@PathVariable String courseId) {
        return courseService.findTeachplanNodeById(courseId);
    }

    @Override
    @PostMapping("/teachplan/add")
    public ResponseResult addTeachplan(@RequestBody Teachplan teachplan) {
        return courseService.addTeachplan(teachplan);
    }

    @Override
    @GetMapping("/coursebase/query/{id}")
    public CourseBase queryCourseBaseById(@PathVariable String id) {
        return courseService.queryCourseBaseById(id);
    }

    @Override
    @PutMapping("/coursebase/edit/{id}")
    public ResponseResult editCourse(@PathVariable String id, @RequestBody CourseBase courseBase) {
        return courseService.editCourse(id,courseBase);
    }
    //@PreAuthorize("hasAuthority('course_find_list')")
    @Override
    @GetMapping("/coursebase/list/{page}/{size}")
    public QueryResponseResult findCourseList(@PathVariable("page") int page, @PathVariable("size") int size,
                                              CourseListRequest courseListRequest, HttpServletRequest request) {
        //String companyId = "1";
        JyxOauth2Util jyxOauth2Util = new JyxOauth2Util();
        JyxOauth2Util.UserJwt userJwt = jyxOauth2Util.getUserJwtFromHeader(request);
        if(userJwt == null){
            ExceptionCast.cast(CommonCode.UNAUTHENTICATED);
        }
        String companyId = userJwt.getCompanyId();
        return courseService.findCourseList(companyId,page,size,courseListRequest);
    }

    @Override
    @PostMapping("/coursebase/add")
    public ResponseResult addCourse(@RequestBody CourseBase courseBase,HttpServletRequest request) {
        JyxOauth2Util jyxOauth2Util = new JyxOauth2Util();
        JyxOauth2Util.UserJwt userJwt = jyxOauth2Util.getUserJwtFromHeader(request);
        if(userJwt == null){
            ExceptionCast.cast(CommonCode.UNAUTHENTICATED);
        }
        String companyId = userJwt.getCompanyId();
        courseBase.setCompanyId(companyId);
        String userId = userJwt.getId();
        courseBase.setUserId(userId);
        courseBase.setStatus("202001");
        return courseService.addCourse(courseBase);
    }

    @Override
    @GetMapping("/courseMarket/query/{id}")
    public CourseMarket getCourseMarketById(@PathVariable("id") String id) {
        return courseMarketService.getCourseMarketById(id);
    }

    @Override
    @PutMapping("/courseMarket/edit/{id}")
    public ResponseResult updateCourseMarket(@PathVariable String id, @RequestBody CourseMarket courseMarket) {
        return courseMarketService.updateCourseMarketById(id,courseMarket);
    }

    @Override
    @PostMapping("/coursepic/add")
    public ResponseResult addCoursePic(@RequestParam("courseId") String courseId, @RequestParam("pic") String pic) {
        return courseService.addCoursePicById(courseId,pic);
    }

    @Override
    @DeleteMapping("/coursepic/delete")
    public ResponseResult delCoursePic(@RequestParam("courseId") String courseId) {

        return courseService.delCoursePicById(courseId);
    }

    @Override
    @GetMapping("/coursepic/list/{id}")
    public CoursePic findCoursePic(@PathVariable("id") String courseId) {
        return courseService.findCoursePic(courseId);
    }

    @Override
    @GetMapping("/courseview/{id}")
    public CourseView courseView(@PathVariable("id") String id) {
        return courseService.getCourseView(id);
    }

    @Override
    @PostMapping("/preview/{id}")
    public CoursePublishResult preview(@PathVariable("id") String id) {
        return courseService.preview(id);
    }

    @Override
    @PostMapping("/publish/{id}")
    public CoursePublishResult publish(@PathVariable  String id) {
        return courseService.publish(id);
    }

    @Override
    @PostMapping("/savemedia")
    public ResponseResult savemedia(@RequestBody TeachplanMedia teachplanMedia) {
        return courseService.savemedia(teachplanMedia);
    }

    @Override
    @GetMapping("/coursepub/get/{id}")
    public CoursePub getCoursePubById(@PathVariable("id") String courseId) {
        return courseService.getCoursePubById(courseId);
    }


}
